
export default {
  reduceCartitem (state, id) {
    state.cart = state.cart.reduce((result, item) => {
      if (item.id === id) {
        item.count--
      }
      if (item.count > 0) {
        result.push(item)
      }
      return result
    }, [])
  },

  addCatitem (state, id) {
    state.cart = state.cart.map(item => {
      if (item.id === id) {
        item.count++
      }
      return item
    })
  },
  detelecat (state, id) {
    state.cart = state.cart.filter(item => item.id !== id)
  },
  addcart (state, item) {
    const {
      id
    } = item
    const addcartItem = state.cart.some((addcartid) => {
      return addcartid.id === id
    })
    if (addcartItem) {
      this.commit('addCatitem', id)
    } else {
      state.cart.push({
        ...item,
        count: 1,
        ischecked: false
      })
    }
  },
  togglechecked (state, id) {
    console.log(this)
    state.cart = state.cart.map(item => {
      if (item.id === id) {
        item.ischecked = !item.ischecked
      }
      return item
    })
  },
  toggleAllchecked (state, event) {
    const { checked } = event.target
    state.cart = state.cart.map(item => {
      item.ischecked = checked
      return item
    })
  },
  loginSuccess (state, users) {
    state.users = {
      ...users,
      isLogin: true
    }
    console.log(state.users)
  },
  loginout (state, users) {
    state.users = {
      ...users,
      isLogin: false
    }
  }
}
